package org.bear.bundle.framework.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.bear.bundle.framework.controller.MessageType.Type;

import com.mysql.jdbc.Connection;

public class ServerController extends MessageController {

	// 数据库连接URL
	public String dburl;
	// 数据库连接用户名
	public String dbusername;
	// 数据库连接密码
	public String dbpassword;
	// 文件抓取存放路径
	public String fileCatchPath;
	// 任务同步频率
	public String taskSynchroniedPattern;

	private static Properties properties = new Properties();

	public ServerController(String osgiName) {
		this.setOsgiName(osgiName);

		initProperties();
	}

	@Override
	public void initProperties() {
		InputStream inputStream = null;
		try {
			inputStream = new FileInputStream(new File("mqServer.properties"));
			properties.load(inputStream);

			brokerUrl = properties.getProperty("server.mq.url").trim();
			username = properties.getProperty("server.mq.username").trim();
			password = properties.getProperty("server.mq.password").trim();
			ip = properties.getProperty("server.ip").trim();

			dburl = properties.getProperty("db.url").trim();
			dbusername = properties.getProperty("db.username").trim();
			dbpassword = properties.getProperty("db.password").trim();

			fileCatchPath = properties.getProperty("file.catch.path").trim();
			taskSynchroniedPattern = properties.getProperty("task.taskSynchroniedPattern");
			
		} catch (Exception e) {
			e.printStackTrace();
			
			this.outinfo("SERVER_INIT_PROPERTIES", "服务端初始化参数异常",
					Type.EXCEPTION, e.toString());
			
		} finally {
			try {
				if (inputStream != null) {
					inputStream.close();
				}
			} catch (IOException e) {
				this.outinfo("SERVER_INIT_PROPERTIES", "服务端关闭IStream异常",
						Type.EXCEPTION, e.toString());
			}
		}

	}

	public String getIP() {
		return ip;
	}

	public Connection getConnection() {
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = (Connection) java.sql.DriverManager.getConnection(dburl,
					dbusername, dbpassword);
		} catch (Exception e) {
			this.outinfo("服务端", "获取数据库连接", Type.EXCEPTION, e.toString());
		}
		return conn;
	}

}
